package com.amumu.drama.common.modules.order.service.impl;

import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.mapper.UmsIntegrationChangeHistoryMapper;
import com.amumu.drama.model.UmsIntegrationChangeHistory;
import com.amumu.drama.model.UmsIntegrationChangeHistoryExample;
import com.amumu.drama.model.UmsMember;
import com.amumu.drama.common.modules.order.service.BonusPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class BonusPointServiceImpl implements BonusPointService {

    @Autowired
    private UmsIntegrationChangeHistoryMapper integrationChangeHistoryMapper;

    @Override
    public int createBonusPointRecord(UmsIntegrationChangeHistory integrationChangeHistory) {
        if (integrationChangeHistory == null) {
            return 0;
        }
        try {
            return integrationChangeHistoryMapper.insert(integrationChangeHistory);
        } catch (Exception e) {
            log.error("BonusPointServiceImpl createBonusPointRecord error" + e);
            throw  e;
        }
    }

    @Override
    public CommonResult<List<UmsIntegrationChangeHistory>> memberBonusPointList(UmsMember member, Integer changeType) {
        UmsIntegrationChangeHistoryExample example = new UmsIntegrationChangeHistoryExample();
        UmsIntegrationChangeHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(member.getId());
        example.setOrderByClause("id desc");
        Optional.ofNullable(changeType)
                .ifPresent(e -> criteria.andChangeTypeEqualTo(changeType));
        List<UmsIntegrationChangeHistory> integrationChangeHistoryList = integrationChangeHistoryMapper.selectByExample(example);
        return CommonResult.success(integrationChangeHistoryList);
    }
}

